package ovm.gui.views;

import com.vaadin.ui.*;
import org.vaadin.addons.lazyquerycontainer.EntityContainer;
import ovm.management.AbstractManager;
import ovm.management.users.UserRules;

import static ovm.application.MainApplication.*;

public abstract class AbstractCatalogView extends AbstractView {

    protected EntityContainer entityContainer;
    protected Class entityClass;

    protected HorizontalSplitPanel splitPanel;
    protected HorizontalLayout filterLayout;
    protected VerticalLayout tableLayout;
    protected VerticalLayout advancedLayout;
    protected HorizontalLayout actionsLayout;
    protected Table table;
    protected Button editButton, editEntityButton, addButton, deleteButton, saveButton, refreshButton;

    protected Boolean editable;

    protected abstract void setContainerProperties ();
    protected abstract void buildTable ();

    // May override
    protected void initializeContainer () {
        entityContainer = AbstractManager.getDefaultEntityContainer(entityClass);
    }

    // May override
    protected int addNewEntity () {
        return AbstractManager.addDefaultCatalogEntity(entityClass);
    }

    // May override
    protected void editEntity(Object tableValue) {
        if (tableValue == null) {
            navigator.showNotification("Не выбран элемент.");
        } else {
            navigator.showNotification("В этой таблице редактирование элементов не поддерживается. " +
                    "Используйте редактирование таблицы.");
        }
    }
    
    // May override
    protected int deleteEntity () {
        if (table.getValue() != null) {
            entityContainer.removeItem(table.getValue());
            entityContainer.commit();
            entityContainer.refresh();
        } else {
            navigator.showNotification("Не выбран элемент для удаления!");
        }
        return 0;
    }
    
    public AbstractCatalogView(Class entityClass, Boolean editable) {
        super();
        this.entityClass = entityClass;
        this.editable = editable;

        initializeContainer();
        setContainerProperties();

        filterLayout = new HorizontalLayout();
        advancedLayout = new VerticalLayout();
        //filterLayout.setWidth("1100px");

        splitPanel = new HorizontalSplitPanel();
        tableLayout = new VerticalLayout();
        table = new Table("Таблица", entityContainer);

        buildTable();

        actionsLayout = new HorizontalLayout();

        editButton = new Button ("Редактировать таблицу", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                table.setEditable(true);

                saveButton.setEnabled(true);
                editButton.setEnabled(false);
            }
        });

        editEntityButton = new Button ("Редактировать элемент", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                editEntity(table.getValue());
            }
        });
        
        addButton = new Button("Добавить", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                entityContainer.commit();
                entityContainer.refresh();

                if (addNewEntity() == AbstractManager.UNSUCCESSFUL) {
                    navigator.showNotification("Не удалось добавить новый элемент");
                }

                entityContainer.refresh();
            }
        });

        deleteButton = new Button("Удалить", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                deleteEntity();
            }
        });
        
        saveButton = new Button ("Сохранить", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                entityContainer.commit();
                entityContainer.refresh();

                table.setEditable(false);

                saveButton.setEnabled(false);
                editButton.setEnabled(true);
            }
        });

        refreshButton = new Button("Обновить", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                entityContainer.refresh();
            }
        });
        
        saveButton.setEnabled(false);

        if (navigator.getCurrentUser() >= UserRules.TEACHER && editable) {
            actionsLayout.addComponent(addButton);
            actionsLayout.addComponent(editButton);
            actionsLayout.addComponent(editEntityButton);
            actionsLayout.addComponent(deleteButton);
            actionsLayout.addComponent(saveButton);
            actionsLayout.addComponent(refreshButton);
            table.setSelectable(true);
        }

        tableLayout.addComponent(filterLayout);
        tableLayout.addComponent(advancedLayout);
        tableLayout.addComponent(table);
        tableLayout.addComponent(actionsLayout);

        if (editable) {
            splitPanel.setFirstComponent(tableLayout);
            addComponent(splitPanel);
        } else {
            addComponent(tableLayout);
        }

    }


}
